const axios = require('axios');

(async () => {
  console.log('🔧 手动修复勋章显示问题');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 获取完整的"全部勋章"和"可获得勋章"列表
    console.log('\n🔍 获取所有勋章...');
    
    const allBadgesResponse = await axios.get('http://localhost:3000/api/badges?limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const allBadges = allBadgesResponse.data.badges;
    
    const availableBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=available&limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const availableBadges = availableBadgesResponse.data.badges;
    
    // 3. 确认当前状态
    console.log(`全部勋章: ${allBadges.length}个`);
    console.log(`可获得勋章: ${availableBadges.length}个`);
    
    // 寻找缺失的勋章
    const missingBadgeIds = ['potential_9', 'potential_10', 'potential_11'];
    
    // 检查是否仍然缺失
    const foundInAll = allBadges.filter(badge => missingBadgeIds.includes(badge.id));
    const foundInAvailable = availableBadges.filter(badge => missingBadgeIds.includes(badge.id));
    
    console.log(`在"全部勋章"中找到目标勋章: ${foundInAll.length}/${missingBadgeIds.length}个`);
    console.log(`在"可获得勋章"中找到目标勋章: ${foundInAvailable.length}/${missingBadgeIds.length}个`);
    
    // 检查分页是否解决了问题
    if (foundInAll.length === missingBadgeIds.length) {
      console.log('✅ 勋章显示问题已修复！分页参数调整解决了问题。');
    } else {
      console.log('❌ 勋章显示问题仍然存在，尝试手动修复...');
      
      // 4. 修复方案：清空浏览器本地缓存
      console.log('\n🔧 建议修复方案:');
      console.log('1. 清空浏览器缓存和本地存储');
      console.log('2. 重新登录应用');
      console.log('3. 重新加载勋章页面');
      
      // 5. 显示最终状态
      console.log('\n📊 最终状态:');
      const potentialInAll = allBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
      const potentialInAvailable = availableBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
      
      console.log(`全部勋章中的潜在勋章: ${potentialInAll}个`);
      console.log(`可获得勋章中的潜在勋章: ${potentialInAvailable}个`);
      
      if (potentialInAll === potentialInAvailable) {
        console.log('✅ 潜在勋章数量一致');
      } else {
        console.log('❌ 潜在勋章数量不一致');
      }
    }
    
  } catch (error) {
    console.error('❌ 操作失败:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 